using System.Collections;
using UnityEngine;

public class Enemy : Entity
{
    [Header("Quest Info")]
    public string questTargetId;
    public Transform player { get; private set; }
    public Enemy_Health enemyHealth { get; private set; }
    
    [Header("Move Info")]
    public float moveSpeed;

    [Header("----- Battle Info -----")]
    public float idleWaitTime;
    public float battleMoveSpeed = 2f;
    public float attackDistance = 2f;
    public float attackCooldown;
    public bool canChase = true;
    public float battleTimeDuration; //战斗状态持续时间
    public float minRetreatDistacnce; //应该执行后撤的最小判定距离
    public Vector2 retreatMovement; //后撤距离
    public float minTraceDistance; //追踪玩家时的最小剩余距离，防止玩家和敌人的x位置相同导致的敌人抽搐
    [Range(0, 2)] public float MoveAnimatorMultiplier = 1;


    public Enemy_IdleState idleState;
    public Enemy_MoveState moveState;
    public Enemy_AttackState attackState;
    public Enemy_BattleState battleState;
    public Enemy_StunnedState stunnedState;

    [Header("----- Player Detect -----")]
    [SerializeField] private Transform playerCheck;
    [SerializeField] private float detectDistance;
    [SerializeField] private LayerMask playerLayer;

    [Header("----- Stunned -----")]
    public float stunnedDuration = 1;
    public Vector2 stunnedMovement = new(7, 7);
    protected bool canStunned = false; //是否能够眩晕


    private float scaleFactor = 1f;
    public float GetMoveSpeed() => moveSpeed * scaleFactor;
    public float GetBattleSpeed() => battleMoveSpeed * scaleFactor;

    #region 生命周期函数
    protected override void Awake()
    {
        base.Awake();
        enemyHealth = GetComponent<Enemy_Health>();
        player = FindObjectOfType<Player>().transform;
    }
    protected virtual void OnEnable()
    {
        Player.OnPlayerDead += HandlePlayerDead;
    }
    protected virtual void OnDisable()
    {
        Player.OnPlayerDead -= HandlePlayerDead;
    }
    protected override void Update()
    {
        base.Update();
        stateMachine.currentState.Update();
        
    }
    #endregion

    protected override IEnumerator SlowDownEntityCo(float duration, float slowMultiplier)
    {
        scaleFactor = 1 - slowMultiplier;
        animator.speed = scaleFactor;
        yield return new WaitForSeconds(duration);
        StopSlowdownEntityCo();
    }

    public override void StopSlowdownEntityCo()
    {
        scaleFactor = 1f;
        animator.speed = scaleFactor;
        base.StopSlowdownEntityCo();
    }
    public virtual void RemoteAttack()
    {
        
    }

    //是否进入眩晕窗口期
    public void OnEnableStunnedWindow(bool canStunned) => this.canStunned = canStunned;
    public void TryEnterBattleState(Transform player)
    {
        if (stateMachine.currentState == battleState || stateMachine.currentState == attackState)
            return;
        this.player = player;
        stateMachine.ChangeState(battleState);
    }
    public RaycastHit2D PlayerDetected()
    {
        RaycastHit2D hit = Physics2D.Raycast(playerCheck.position, Vector2.right * facingDir, detectDistance, playerLayer | whatIsGround);
        if (hit.collider == null || hit.collider.gameObject.layer != LayerMask.NameToLayer("Player"))
        {
            return default;
        }
        return hit;
    }
    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(playerCheck.position, new Vector3(playerCheck.position.x + detectDistance * facingDir, playerCheck.position.y, playerCheck.position.z));
        Gizmos.color = Color.red;
        Gizmos.DrawLine(playerCheck.position, new Vector3(playerCheck.position.x + attackDistance * facingDir, playerCheck.position.y, playerCheck.position.z));
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(playerCheck.position, new Vector3(playerCheck.position.x + minRetreatDistacnce * facingDir, playerCheck.position.y, playerCheck.position.z));
    }
    private void HandlePlayerDead()
    {
        stateMachine.ChangeState(idleState);
    }
}
